<?php
session_start();
require_once ('../init.php');
require_once ('includes/lib/func_rightCtrl.php');
// 权限控制
admin_rightCtrl();
if($_SESSION['level'] == 0){
	require_once ('includes/views/root_header.php');
}
elseif ($_SESSION['level'] == 1) {
	require_once ('includes/views/admin_header.php');
}
echo <<<tableheader
<link href="/afctf/css/announce.css" rel="stylesheet" type="text/css" media="all"/>
<div class="challenge_table">
	<blockquote class="layui-elem-quote">答题情况</blockquote>
	<table border="1" class="layui-table">
		<thead>
		  <tr align="center">
		    <th>序号</th>
		    <th>题目标题</th>
		    <th>题目类别</th>
		    <th>攻破时间</th>
		    <th>一血选手</th>
		    <th>解出人数</th>
		  </tr>
		</thead>
		<tbody>
tableheader;
// 查询语句
// 1.获取答对题目名字和答对人数
$solvedTitle_query = "SELECT `title`,count(`title`) AS solvedCount FROM `solved_situation` GROUP BY `title` ORDER BY solvedCount ASC";
$sth_sTitle  = $dbh -> prepare($solvedTitle_query);
$sth_sTitle	 ->execute();
$solvednum 	 = $sth_sTitle->rowCount();
$solvedTitle  = $sth_sTitle -> fetchAll();
if($solvednum === 0){
	echo <<<EOT
	<tr align="center">
	    <td colspan="8">暂无数据</td>
	</tr>
EOT;
}
// 2.获取每道题一血选手信息
else{
	//表格内容
	for($i = 0;$i < $solvednum; $i++){
		$counter = $i + 1;

		$theTitle = $solvedTitle[$i]['title'];
		$theCount = $solvedTitle[$i]['solvedCount'];

		$taskInfo_query = "SELECT `tid`,`uid`,`submit_time`,`type` FROM `solved_situation` WHERE `title` = :theTitle ORDER BY `submit_time` ASC limit 0,1";
		$sth_task  = $dbh -> prepare($taskInfo_query);
		$sth_task ->bindParam(":theTitle",$theTitle);
		$sth_task ->execute();
		$taskInfo  = $sth_task -> fetch();

		$theTid = $taskInfo['tid'];
		$theUid = $taskInfo['uid'];
		$theTime = $taskInfo['submit_time'];
		$theType = $taskInfo['type'];

		// 3.查询选手昵称
		$nickname_query = "SELECT `nickname` FROM `user` WHERE `uid` = :theUid";
		$sth_nname  = $dbh -> prepare($nickname_query);
		$sth_nname ->bindParam(":theUid",$theUid);
		$sth_nname ->execute();
		$theNickname  = $sth_nname -> fetch()['nickname'];
		echo <<<EOT
		  <tr align="center">
		    <td>$counter</td>
		    <td><a href="javascrip:void(0)" onclick="taskSolver($theTid)" target="_self">$theTitle</a></td>
		    <td>$theType</td>
		    <td>$theTime</td>
		    <td><a href="/afctf/admin/playerInfo.php?uid=$theUid">$theNickname</a></td>
		    <td>$theCount</td>
		  </tr>
EOT;
	}
}
//表格尾
echo <<<EOT
		</tbody>
	</table>
</div>
<script type="text/javascript">
	$("#a_home").removeClass("active");
	$("#a_solved").addClass("active");
	function taskSolver(tid){
		var url = '/afctf/admin/taskSolver.php?tid=';
		url = url + tid;
		layer.open({
			type: 2,
			title: '该题解题情况',
			shadeClose:true,
			area: ['500px','400px'],
			content: url
		});
	}
</script>
EOT;
require_once ('includes/views/footer.php');
?>